/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.bid.service.taskplugins  
 */
package com.apache.bid.service.taskplugins;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;

import com.apache.api.api.TimeTaskService;
import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.bid.common.Constant;
import com.apache.bid.common.cache.BidCacheFactory;
import com.apache.bid.common.cache.BidCacheManager;
import com.apache.bid.entity.BidApply;
import com.apache.bid.entity.BidApplyHistory;
import com.apache.bid.entity.BidItem;
import com.apache.bid.entity.BidSession;
import com.apache.bid.manager.BidApplyHistoryManager;
import com.apache.bid.manager.BidApplyManager;
import com.apache.bid.manager.BidItemManager;
import com.apache.bid.manager.BidSessionManager;
import com.apache.cache.util.Validator;

/**
 * description:  结束活动定时器
 * @author Hou Dayu 创建时间：2016-4-7  
 * 
 */
public class EndSessionTask implements PluginConnector, TimeTaskService {

	private transient static boolean EndSessionTaskMark = true;

	private BidSessionManager bidSessionManager;
	private BidItemManager bidItemManager;

	private BidApplyManager bidApplyManager;
	private BidApplyHistoryManager bidApplyHistoryManager;

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.zbxsoft.api.api.TimeTaskService#run()  
	 */
	public boolean run() {
		try {
			execute(new ParamsVo());
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.zbxsoft.api.manager.PluginConnector#execute(com.zbxsoft.api.vo.ParamsVo)  
	 */
	public Object execute(ParamsVo param) throws Exception {
		ResultEntity entity = new ResultEntity();
		try {
			if (EndSessionTaskMark) {
				EndSessionTaskMark = false;
				param.setMethodKey("BySessinIds");
				BidSession bse = new BidSession();
				bse.setStatus(Constant.getInstance().getSessionConfig("statusPubslish"));
				param.setObj(bse);
				List list = bidSessionManager.getList(param);
				if (Validator.isEmpty(list)) {
					return entity;
				}
				int count = list.size();
				BidCacheManager cache = BidCacheFactory.getInstance().getCacheManager("session");
				ParamsVo<BidItem> itemVo = new ParamsVo<BidItem>();
				ParamsVo<BidSession> svo = new ParamsVo<BidSession>();
				BidItem item = new BidItem();
				for (int i = 0; i < count; i++) {
					try {
						String seId = String.valueOf(list.get(i));
						item.setSessionId(seId);
						itemVo.setObj(item);
						long itemNum = bidItemManager.countInfo(itemVo);
						if (itemNum == 0) {
							BidSession session = (BidSession) cache.getCacheCloneByKey(cache.session_prefix + seId);
							session.setStatus(Constant.getInstance().getSessionConfig("statusStop"));
							svo.setObj(session);
							if (Constant.SETYPE_ZC.equals(session.getSessionType())) {
								saveApplys(session.getSessionId());
							}
							boolean mark = bidSessionManager.editInfo(svo);
							if (mark) {
								cache.removeCacheObject(cache.session_prefix + seId);
							}
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		} finally {
			EndSessionTaskMark = true;
		}
		return entity;
	}

	/**
	 * description:  批量移申请记录
	 */
	private void saveApplys(String sessionId) {
		ParamsVo vo = new ParamsVo();
		BidApply apply = new BidApply();
		apply.setSessionId(sessionId);
		vo.setObj(apply);
		List<BidApply> list = bidApplyManager.getList(vo);
		if (!Validator.isEmpty(list)) {
			List<BidApplyHistory> apps = new ArrayList<BidApplyHistory>();
			for (BidApply ay : list) {
				BidApplyHistory bah = new BidApplyHistory();
				BeanUtils.copyProperties(ay, bah);
				apps.add(bah);
			}
			vo.setObj(null);
			vo.setObjs(apps);
			bidApplyHistoryManager.saveInfo(vo);
			vo.setObj(apply);
			bidApplyManager.deleteInfo(vo);
		}
	}

	public void setBidSessionManager(BidSessionManager bidSessionManager) {
		this.bidSessionManager = bidSessionManager;
	}

	public void setBidItemManager(BidItemManager bidItemManager) {
		this.bidItemManager = bidItemManager;
	}

	public void setBidApplyManager(BidApplyManager bidApplyManager) {
		this.bidApplyManager = bidApplyManager;
	}

	public void setBidApplyHistoryManager(BidApplyHistoryManager bidApplyHistoryManager) {
		this.bidApplyHistoryManager = bidApplyHistoryManager;
	}

}
